import json
import traceback
from django.http import JsonResponse
from AiP.services import crawlerService
from django.views.decorators.csrf import csrf_exempt

@csrf_exempt
def runCrawler(request):
    res = {"code": 200, "data": {}, "message": "Success"}
    try:
        params = request.GET if len(request.body) == 0 else json.loads(request.body)
        method = params.get("method")
        url = params.get("url")
        headers = params.get("headers")
        cssSelector = params.get("cssSelector")
        regularSelector = params.get("regularSelector")
        
        if not method: raise Exception("method不能为空")
        if not url: raise Exception("url不能为空")
        
        res["data"] = crawlerService.runCrawler(method, url, headers=headers, cssSelector=cssSelector, regularSelector=regularSelector)
    except Exception as e:
        print(traceback.format_exc())
        res["code"] = 400
        res["message"] = "未知错误，请联系管理员"
    finally:
        return JsonResponse(res)
